/**
 * 
 */
package com.keggview.application.xml.datatypes;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

//import org.dom4j.Element;

/**
 * @author Pablo
 * 
 */
@XStreamAlias("graphics")
public class Graphics extends Base {

	
	public static final String T_RECT = "rectangle";
	public static final String T_CIRC = "circle";
	public static final String T_ROUNRECT = "roundrectangle";
	
	public static final String C_FCOLOR = "#000000";
	public static final String C_BCOLOR = "#FFFFFF";
	public static final String C_BCOLOR_GENE = "#BFFFBF";

	/**
	 * (implied) The name attribute contains the label that is associated with
	 * this graphics object. When two or more name attributes are specified in
	 * the same entry element, the first one is taken as the attribute value.
	 * When the type attribute value of the entry element is "gene", the gene
	 * name is specified for this attribute value.<br>
	 */
	@XStreamAsAttribute
	String name;

	/**
	 * (implied) The x attribute specifies the x-coordinate value of this
	 * graphics object in the manually drawn KEGG pathway map.<br>
	 * format:<br>
	 * positive integer
	 */
	@XStreamAsAttribute
	Integer x;

	/**
	 * (implied) The y attribute specifies the y-coordinate value of this
	 * graphics object in the manually drawn KEGG pathway map.<br>
	 * format:<br>
	 * positive integer
	 */
	@XStreamAsAttribute
	Integer y;

	/**
	 * (implied) The type attribute specifies the shape of this object. The
	 * default value is "rectangle". format: - rectangle : the shape is a
	 * rectangle, which is used to represent a gene product and its complex
	 * (including an ortholog group).<br> - circle : the shape is a circle,
	 * which is used to specify any other molecule such as a chemical compound
	 * and a glycan.<br> - roundrectangle : the shape is a round rectangle,
	 * which is used to represent a linked pathway.
	 */
	@XStreamAsAttribute
	String type;

	/**
	 * (implied) The width attribute specifies the width this object. The
	 * default value is "45".<br>
	 * format:<br>
	 * positive integer
	 */
	@XStreamAsAttribute
	Integer width = 45;

	/**
	 * (implied) The height attribute specifies the height of this object. The
	 * default value is "17".<br>
	 * format:<br>
	 * positive integer
	 */
	@XStreamAsAttribute
	Integer height = 17;

	/**
	 * (implied) The fgcolor attribute specifies the foreground color of this
	 * object. It applies to the frame and the character string. The default
	 * value is "#000000".<br>
	 * format:<br>
	 * numerical RGB #000000
	 */
	@XStreamAsAttribute
	String fgcolor = "#000000";

	/**
	 * (implied) The bgcolor attribute specifies the background color of this
	 * object. The default value is "#FFFFFF". The background color for the gene
	 * product is "#BFFFBF". format:<br>
	 * numerical RGB #000000
	 */
	@XStreamAsAttribute
	String bgcolor = "#BFFFBF";

	private Graphics() {
		Registry.register(this);
	}
	/**
	 * @param name
	 * @param x
	 * @param y
	 * @param type
	 * @param width
	 * @param height
	 * @param fgcolor
	 * @param bgcolor
	 */
	public Graphics(String name, Integer x, Integer y, String type,
			Integer width, Integer height, String fgcolor, String bgcolor) {

		this();
		if (name == null) {
			this.name = new String("");
		} else {
			this.name = name;
		}

		if (x == null || x < 0) {
			this.x = new Integer((int) Math.round(Math.random() * (800)));
		} else {
			this.x = x;
		}

		if (y == null || y < 0) {
			this.y = new Integer((int) Math.round(Math.random() * (600)));
		} else {
			this.y = y;
		}

		this.type = this.validateType(type);

		if (width == null || width < 0) {
			this.width = new Integer(45);
		} else {
			this.width = width;
		}
		
		if (height == null || height < 0) {
			this.height = new Integer(17);
		} else {
			this.height = height;
		}
		
		if (fgcolor == null) {
			this.fgcolor = validateColor(Graphics.C_FCOLOR, Graphics.C_FCOLOR);
		} else {
			this.fgcolor = validateColor(fgcolor, Graphics.C_FCOLOR);
		}
		
		if (bgcolor == null) {
			this.bgcolor = validateColor(Graphics.C_BCOLOR, Graphics.C_BCOLOR);
		} else {
			this.bgcolor = validateColor(bgcolor, Graphics.C_BCOLOR);
		}
	}

//	public Graphics(Element element) {
//		this();
//		this.bgcolor = this.validateColor(element.attribute("bgcolor").getValue(), Graphics.C_BCOLOR);
//		this.fgcolor = this.validateColor(element.attribute("fgcolor").getValue(), Graphics.C_FCOLOR);
//		this.height = Integer.valueOf(element.attribute("height").getValue());
//		this.name = element.attribute("name").getValue();
//		this.type = element.attribute("type").getValue();
//		this.width = Integer.valueOf(element.attribute("width").getValue());
//		this.x = Integer.valueOf(element.attribute("x").getValue());
//		this.y = Integer.valueOf(element.attribute("y").getValue());
//	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the x
	 */
	public Integer getX() {
		return x;
	}

	/**
	 * @param x
	 *            the x to set
	 */
	public void setX(Integer x) {
		this.x = x;
	}

	/**
	 * @return the y
	 */
	public Integer getY() {
		return y;
	}

	/**
	 * @param y
	 *            the y to set
	 */
	public void setY(Integer y) {
		this.y = y;
	}

	/**
	 * @return the type
	 */
	public String getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(String type) {
		this.type = this.validateType(type);
	}

	/**
	 * @return the width
	 */
	public Integer getWidth() {
		return width;
	}

	/**
	 * @param width
	 *            the width to set
	 */
	public void setWidth(Integer width) {
		this.width = width;
	}

	/**
	 * @return the height
	 */
	public Integer getHeight() {
		return height;
	}

	/**
	 * @param height
	 *            the height to set
	 */
	public void setHeight(Integer height) {
		this.height = height;
	}

	/**
	 * @return the fgcolor
	 */
	public String getFgcolor() {
		return fgcolor;
	}

	/**
	 * @param fgcolor
	 *            the fgcolor to set
	 */
	public void setFgcolor(String fgcolor) {
		this.fgcolor = fgcolor;
	}

	/**
	 * @return the bgcolor
	 */
	public String getBgcolor() {
		return bgcolor;
	}

	/**
	 * @param bgcolor
	 *            the bgcolor to set
	 */
	public void setBgcolor(String bgcolor) {
		this.bgcolor = bgcolor;
	}

	public String validateType(String type) {
		if (type == null || type.length() == 0) {
			return Graphics.T_RECT;
		} else {
			if (type.equalsIgnoreCase(Graphics.T_CIRC)) {
				return Graphics.T_CIRC;
			}

			if (type.equalsIgnoreCase(Graphics.T_ROUNRECT)) {
				return Graphics.T_ROUNRECT;
			}
		}
		return Graphics.T_RECT;
	}
	
	/**
	 * checks if value of color which we wants to set is correct, if not set
	 * as default color second value
	 * 
	 *  
	 * @param newColor
	 * @param defaultC
	 * @return
	 */
	public String validateColor(String newColor, String defaultC) {
		String pattern = "(#)([0-9a-f]{6})";
		String correctColor = defaultC;
		if (newColor.matches(pattern)) {
			correctColor = newColor;
		}
		return correctColor;
	}

}
